package com.sulj.domain;

import com.sulj.enums.CouponType;

import java.math.BigDecimal;

/**
 * 满减券类
 *
 * @author slj
 * @date 2025-03-30
 */
public class FullReductionCoupon extends Coupon {

    public FullReductionCoupon(String code, BigDecimal minAmount, BigDecimal discountValue) {
        super(CouponType.FULL_REDUCTION, code, minAmount, discountValue);
        if (discountValue.compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("满减优惠价要大于0");
        }
    }


    /**
     * 计算优惠价格
     *
     * @param order 订单
     * @return 优惠后的价格
     */
    @Override
    public BigDecimal applyDiscount(Order order) {
        if (!validate(order.getTotalAmount())) {
            return order.getTotalAmount();
        }
        //防负值处理‌
        if (order.getTotalAmount().compareTo(discountValue) < 0) {
            return BigDecimal.ZERO;
        }
        return order.getTotalAmount().subtract(discountValue);
    }
}
